package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //分页查找
        Page<SetmealVO> setmealVO = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(setmealVO.getTotal(),setmealVO.getResult());
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //需要修改 2张表 setmeal setmeal_dish
        //获取分类标表排序
        //插入套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //插入套餐菜品表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            });
        }


    }

    @Override
    public SetmealVO getById(Long id) {
        //获取套餐、多表查询
        log.info("获取套餐的基本信息");
        Setmeal setmeal = setmealMapper.getById(id);
        //获取套餐菜品数据
        log.info("获取菜品信息回显");
        List<SetmealDish> setmealDish = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //需要修改两张表setmeal、setmeal_dish
        //修改套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        ShoppingCart cart = ShoppingCart.builder().amount(setmeal.getPrice()).dishId(setmeal.getId()).image(setmeal.getImage()).name(setmeal.getName()).build();
        shoppingCartMapper.updateBySetmealId(cart);
        //修改后的菜品信息需要覆盖之前的信息
        //先删除再添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishMapper.deleteById(setmeal.getId());
            for (SetmealDish dish : setmealDishes) {
                //不需要修改绑定的id套餐
                dish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(dish);
            }
        }


    }

    @Override
    public void updateStatus(Integer status,Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
        //还需要删除用户购物车中的数据
        shoppingCartMapper.deleteBySetmealId(id);
    }

    @Override
    @Transactional
    public void  deleteByIds(Long[] ids) {
        //套餐删除之后，需要删除两张表的内容 setmeal setmeal_dish
        //需要判断套餐是否处于热卖，如果热卖不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == 1){
                throw new DeletionNotAllowedException("套餐热卖中，无法删除");
            }
            //删除套餐
            setmealMapper.deleteById(id);
            //删除套餐菜品表中所对应的菜品
            setmealDishMapper.deleteById(id);
            //还需要删除用户购物车中的数据
            shoppingCartMapper.deleteBySetmealId(id);
        }
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public List<SetmealVO> getByCategoryId(Long categoryId) {
        List<SetmealVO> list = setmealMapper.getByCategoryId(categoryId);
        return list;
    }

}
